<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 11/15/18
 * Time: 5:48 PM
 */

namespace caroltc\jenkinsapp\controller;


use caroltc\jenkinsapp\library\DbHandler;
use caroltc\jenkinsapp\library\JenkinsSDK;

class JenkinsHandler extends BaseController
{
    /**
     * @var JenkinsSDK
     */
    private $JenkinsSDK = null;

    public function __construct()
    {
        parent::__construct();
        $this->checkUserSession();
        $this->initJenkinsConfig();
    }

    /**
     * 根据jenkins_config_id获取配置信息
     * @return null
     */
    private function initJenkinsConfig()
    {
        $this->checkParams(['jenkins_config_id']);
        $jenkins_config = (new DbHandler())->getJenkinsConfigByUserIdAndConfigId($this->user_id, $this->getRequestParam('jenkins_config_id'));
        if (empty($jenkins_config)) {
            return $this->responseFail(40000, 'jenkins config is invalid');
        }
        $this->JenkinsSDK = new JenkinsSDK($jenkins_config['jenkins_url'], $jenkins_config['jenkins_username'], $jenkins_config['jenkins_password'], $jenkins_config['jenkins_api_token']);
        return null;
    }

    /**
     * 获取构建job列表
     * @return null
     */
    public function getJobList()
    {
        try {
            return $this->responseOk(['result' => $this->JenkinsSDK->getJobList()]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }

    }

    /**
     * 获取构建job详情
     * @return null
     */
    public function getJobDetail()
    {
        $this->checkParams(['job_name']);
        try {
            return $this->responseOk(['result' => $this->JenkinsSDK->getJobDetail($this->getRequestParam('job_name'))]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 构建
     * @return null
     */
    public function buildJob()
    {
        $this->checkParams(['job_name']);
        try {
            return $this->responseOk(['result' => $this->JenkinsSDK->buildJob($this->getRequestParam('job_name'), $this->getRequestParam('params', []))]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 停止job
     * @return null
     */
    public function stopJob()
    {
        $this->checkParams(['job_name', 'history_number']);
        try {
            $result = $this->JenkinsSDK->stopJob($this->getRequestParam('job_name'), $this->getRequestParam('history_number'));
        } catch (\Exception $e) {
            $result = $e->getMessage();
        }
        return $this->responseOk(['result' => $result]);
    }

    /**
     * 获取job指定构建历史详情
     * @return null
     */
    public function getJobBuildHistory()
    {
        $this->checkParams(['job_name', 'history_number']);
        try {
            return $this->responseOk(['result' => $this->JenkinsSDK->getJobBuildHistory($this->getRequestParam('job_name'), $this->getRequestParam('history_number'))]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

}